package cn.yunhe.test;

import cn.yunhe.entity.CollectionBean;
import cn.yunhe.entity.Computer;
import cn.yunhe.entity.User;
import com.sun.javafx.collections.MappingChange;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.*;

public class Demo1 {

    ApplicationContext ac = null;

    @Before
    public void init() {
        ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    }


    @Test
    public void user1() {
        //例一：是否包含某个bean
        ac.containsBean("user");
        User user = (User) ac.getBean("user");
        user.setName("zhangsan");
        user.setAge(10);
        System.out.println(user.printInfo());

    }

    @Test
//  例二 设值注入
    public void test2() {
        Computer comp = (Computer) ac.getBean("computer");
        comp.com();

    }

    @Test
//    例二 构造注入
    public void test3() {
        Computer cc = ac.getBean("computer3", Computer.class);
        cc.com();

    }

    @Test
    //    例三 集合
    public void test4() {
        CollectionBean cb = (CollectionBean) ac.getBean("coll");
        //List集合的遍历
        List list = cb.getList();
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));

        }
        //Set集合的遍历
        Iterator it = cb.getSet().iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }

        //Map集合的遍历
        Map map = cb.getMap();
        Set keySet = map.keySet();
        Iterator en = keySet.iterator();
        while (en.hasNext()) {

            Object key = en.next();
            Object value = map.get(key);
            System.out.println("key" + key + "value" + value);

        }


        //Properties集合的遍历
        Properties p = cb.getProp();
        Enumeration names =p.propertyNames();
        while (names.hasMoreElements()) {
            String key = (String) names.nextElement();
            String value = p.getProperty(key);
            System.out.println(key + "," + value);
        }


    }


}
